<template>
  <div>
    <el-form :model="userInfo" :rules="userRules" class="form-style">
      <el-form-item label="姓名" prop="UserName" style="margin-bottom: 20px">
        <el-input v-model="userInfo.UserName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="Password" style="margin-bottom: 20px">
        <el-input v-model="userInfo.Password"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="Age" style="margin-bottom: 20px">
        <el-input v-model="userInfo.Age"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="Address" style="margin-bottom: 20px">
        <el-input v-model="userInfo.Address"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" style="float: left" @click="LoadData">加载数据</el-button>
    <el-button type="success" style="float: left" @click="ChangeData">修改数据</el-button>
  
  </div>
</template>

<script>
export default {
  data() {
    let verifyAddr = (rule, value, callback) => {
      if (value === "" || value === null || value === undefined) {
        return callback("地址不能为空");
      }
      return callback();
    };
    return {
      userInfo: {},
      userRules: {
        UserName: [
          { required: true, message: "幢名不能为空", trigger: "blur" },
        ],
        Age: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        Address: [{ required: true, validator: verifyAddr, trigger: "blur" }],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    LoadData() {
      let obj = { UserName: "zs", Age: 18, Address: "山东省济宁市金乡县" };
      let obj2 = { Password: "123456" };
      let json = JSON.stringify(obj2);
      let res = JSON.parse(json);

      this.userInfo = res;  // 这行代码执行之后，res上添加了 get Password函数与set Password函数，分别是 reactiveGetter 与 reactiveSetter
      //this.userInfo = Object.assign(this.userInfo,obj); // 但是这行代码执行之后，并不会为 this.userInfo 新增的属性添加set与get方法，这是为什么？
      this.userInfo = Object.assign(this.userInfo,obj); 
      this.userInfo = Object.assign({},obj);
      // 如果你需要手动添加响应式：
    //   for (let key in obj) {
    //     this.$set(this.userInfo, key, obj[key]);  // 这样确保 obj 中的属性变成响应式
    //   } 

      console.log(this.userInfo);
    },
    ChangeData(){
        this.userInfo.UserName = 'ls';  // 只修改 UserName 时，值不会变化
        this.userInfo.Password = '654321';  // 同时修改带 ReactiveSetter 方法的Password时，也会通知页面UserName的变化
    }
  },
};
</script>

<style lang="scss" scoped>
.form-style {
  width: 600px;
}
</style>